package com.cskaoyan.service;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.CouponExample;
import com.cskaoyan.bean.CouponUser;
import com.cskaoyan.bean.CouponUserExample;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.cskaoyan.vo.CouponUserVo;
import com.cskaoyan.vo.CouponVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponMapper couponMapper;

    @Autowired
    CouponUserMapper couponUserMapper;


    //list相关
    @Override
    public CouponVo query(Integer page, Integer limit, String sort, String order) {
        //分页和排序
        //页码，数据量
        PageHelper.startPage(page,limit);//使用分页插件，mybatis在构建sql语句中，会构造limit和offset

        //构造一个条件
        CouponExample couponExample = new CouponExample();
        couponExample.setOrderByClause(sort + " " + order);//排序条件${}做sql语句拼接
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);

        //total是满足条件的所有数据量，而不是当前的limit
        //可以通过pageInfo获得分页信息
        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);//查询结果放进来
        long total = couponPageInfo.getTotal();//总的数据量 =》selcet count(*)条件是byExample

        CouponVo couponVo = new CouponVo();
        couponVo.setItems(coupons);
        couponVo.setTotal(total);

        return couponVo;
    }

    //listUser相关
    @Override
    public CouponUserVo couponListUser(Integer page, Integer limit, Integer couponId, String sort, String order) {
       PageHelper.startPage(page,limit);

       //构造条件
        CouponUserExample couponUserExample = new CouponUserExample();
        couponUserExample.setOrderByClause(sort + " " + order);
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();

        //couponI不为null
        if (couponId != null){
            criteria.andCouponIdEqualTo(couponId);
        }

        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);

        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>(couponUsers);
        long total = couponUserPageInfo.getTotal();

        CouponUserVo couponUserVo = new CouponUserVo();
        couponUserVo.setItems(couponUsers);
        couponUserVo.setTotal(total);

        return couponUserVo;
    }

    //create相关
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
    @Override
    public Coupon couponCreate(Coupon coupon) {
        Date date = new Date();
        coupon.setAddTime(date);
        coupon.setUpdateTime(date);

        couponMapper.insertSelective(coupon);
        Integer id = coupon.getId();
        Coupon data = couponMapper.selectByPrimaryKey(id);
        return data;
    }

    //update相关
    @Override
    public Coupon couponUpdate(Coupon coupon) {
        int i = couponMapper.updateByPrimaryKey(coupon);
        Integer id = coupon.getId();
        Coupon data = couponMapper.selectByPrimaryKey(id);
        return data;
    }

    //delete相关
    @Override
    public void couponDelete(Coupon coupon) {
        couponMapper.deleteByPrimaryKey(coupon.getId());
    }

    //read相关
    @Override
    public Coupon selectCouponById(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }
}
